package com.example.cms.Service.Impl;

import com.example.cms.Service.IUploadService;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/11/4-11-04-10:06
 * @Description：com.example.cms.Service.Impl
 */
@Service
public class UploadServiceImpl implements IUploadService {
    // tracker 追踪 文件服务器
    // storage 存文件
    @Autowired
    private FastFileStorageClient storageClient;
    @Override
    public String upload(MultipartFile file) {
        // hello.jpg
        String originalFilename = file.getOriginalFilename();
        // groupName 组
        // inputstream 文件的流形式
        // size 文件的大小
        // fileExtName 文件后缀 jpg/png
        try {
            // 文件返回的路径 /group1/00/00/文件.jpg
            StorePath storePath = storageClient.uploadFile(
                    "group1",
                    file.getInputStream(), file.getSize(), "jpg");
            // 主机+端口+返回路径 默认tracker端口8888
            String path = "http://192.168.18.132:8888/"+storePath.getFullPath();
            System.out.println("path = " + path);
            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
